package com.colorful.insuranceproject.microDrug;

import android.os.Bundle;
import android.os.Message;
import android.support.annotation.NonNull;
import android.widget.EditText;

import com.colorful.insuranceproject.R;
import com.colorful.insuranceproject.base.TitleActivity;
import com.colorful.insuranceproject.consts.CodeTable;
import com.colorful.insuranceproject.request.MicroDrugRequest;
import com.lovely3x.common.utils.Response;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by handsome-Bob on 2017/8/18.
 * <p>
 * 支付密码设置
 */
public class PayPwdSetActivity extends TitleActivity {

    private static final int SET_PAY_PWD_URL = 0X1;

    public static final String EXTRA_ID_CARD = "extra_id_card";

    private MicroDrugRequest microDrugRequest;

    /**
     * 第一次输入的密码
     */
    @Bind(R.id.et_pwd)
    EditText etPayPwd;

    /**
     * 第二次输入的密码
     */
    @Bind(R.id.et_pay_pwd_again)
    EditText etPayPwdAgain;

    private String idCard;

    @Override
    protected int getContentView() {
        return R.layout.activity_pay_pwd_set;
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this);
        setTitle(getString(R.string.pay_pwd));
    }

    @Override
    protected void onViewInitialized() {
        microDrugRequest = new MicroDrugRequest(getHandler());
    }

    @Override
    protected void onInitExtras(@NonNull Bundle bundle) {
        super.onInitExtras(bundle);
        idCard = bundle.getString(EXTRA_ID_CARD);
    }

    @Override
    protected void handleResponseMessage(Message msg, Response response) {
        super.handleResponseMessage(msg, response);
        switch (msg.what){
            case SET_PAY_PWD_URL:
                if (response.isSuccessful){
                    showToast(getString(R.string.set_pay_pwd_success));
                    setResult(RESULT_OK);
                    finish();
                }else{
                    showToast(CodeTable.getInstance().getCodeDescription(response));
                }
                break;
        }
    }

    @Override
    public void restoreInstanceOnCreateBefore(@NonNull Bundle savedInstance) {

    }

    @Override
    public void restoreInstanceOnCreateAfter(@NonNull Bundle savedInstance) {

    }

    @OnClick(R.id.tv_set)
    void OnClicked() {
        String pwd = etPayPwd.getText().toString().trim();
        String pwdAgain = etPayPwdAgain.getText().toString().trim();
        if (pwd.length() != 8){
            showToast(getString(R.string.please_input_length_of_8_pwd));
        }else if (!pwd.equals(pwdAgain)){
            showToast(getString(R.string.The_two_password_input_is_inconsistent));
        }else{
            microDrugRequest.setPayPwd(pwdAgain,idCard,SET_PAY_PWD_URL);
        }
    }
}
